/*
 * Author:  Copyright (c) 2013 Toby
 * E-Mail:  smurffit@gmail.com
 * Web:     https://code.google.com/p/chrome-mfi-filter4kiva/
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, see <http://www.gnu.org/licenses/>.
 */
'use strict';

$(function()
{
	$(document).tooltip();
	$('#tabs').tabs();

	$("#save").button({
		icons : {
			primary : "ui-icon-disk"
		}
	}).next().button({
		icons : {
			primary : "ui-icon-refresh"
		}
	});

	$("#reset").button({
		icons : {
			primary : "ui-icon-trash"
		}
	});

	$("#importDb").button({
		icons : {
			primary : "ui-icon-folder-open"
		}
	});

	$("#dbdl").button({
		icons : {
			primary : "ui-icon-print"
		}
	});

	$("#accordion").accordion({
		heightStyle : "content"
	});

	$("#filterRed").button({
		icons : {
			primary : "ui-icon-alert"
		}
	}).next().button({
		icons : {
			primary : "ui-icon-notice"
		}
	}).next().button({
		icons : {
			primary : "ui-icon-help"
		}
	});

	$("#filterKivaNon").button({
		icons : {
			primary : "ui-icon-battery-0"
		}
	}).next().button({
		icons : {
			primary : "ui-icon-battery-1"
		}
	}).next().button({
		icons : {
			primary : "ui-icon-battery-2"
		}
	});

	$("#filterAllOff").button({
		icons : {
			primary : "ui-icon-document"
		}
	});

	$("#filterKivaThree").button({
		icons : {
			primary : "ui-icon-battery-3"
		}
	}).next().button({
		icons : {
			primary : "ui-icon-star"
		}
	}).next().button({
		icons : {
			primary : "ui-icon-star"
		}
	});
	$("#filterReligious").button({
		icons : {
			primary : "ui-icon-lightbulb"
		}
	});
	$("#highlightTop").button({
		icons : {
			primary : "ui-icon-star"
		}
	}).next().button({
		icons : {
			primary : "ui-icon-star"
		}
	}).next().button({
		icons : {
			primary : "ui-icon-star"
		}
	});

	$("#highlightNonReligious").button({
		icons : {
			primary : "ui-icon-lightbulb"
		}
	}).next().button({
		icons : {
			primary : "ui-icon-script"
		}
	}).next().button({
		icons : {
			primary : "ui-icon-heart"
		}
	});

	$("#highlightAllOff").button({
		icons : {
			primary : "ui-icon-document"
		}
	}).next().button({
		icons : {
			primary : "ui-icon-battery-0"
		}
	}).next().button({
		icons : {
			primary : "ui-icon-battery-1"
		}
	});

	$("#highlightNonReligiousOff").button({
		icons : {
			primary : "ui-icon-lightbulb"
		}
	}).next().button({
		icons : {
			primary : "ui-icon-script"
		}
	}).next().button({
		icons : {
			primary : "ui-icon-heart"
		}
	});
	$("#save2").button({
		icons : {
			primary : "ui-icon-disk"
		}
	});
});

btnSave1.addEventListener('click', function()
{
	save_options(function()
	{
		//
	});
});

btnSave2.addEventListener('click', function()
{
	save_options(function()
	{
		//
	});
});

document.getElementById('sortFilter').onclick = function(e)
{
	createCheckbox(sortTable(res, 8), function()
	{
		// 
	});
};

document.getElementById('sortHighlight').onclick = function(e)
{
	createCheckbox(sortTable(res, 16), function()
	{
		// 
	});
};

document.getElementById('sortPartnerId').onclick = function(e)
{
	createCheckbox(sortTable(res, 2), function()
	{
		// 
	});
};

document.getElementById('sortPartnerName').onclick = function(e)
{
	createCheckbox(sortTable(res, 1), function()
	{
		// 
	});
};

document.getElementById('sortScore').onclick = function(e)
{
	createCheckbox(sortTable(res, 32), function()
	{
		// 
	});
};

document.getElementById('sortRating').onclick = function(e)
{
	createCheckbox(sortTable(res, 4), function()
	{
		// 
	});
};

document.getElementById('fetch').onclick = function(e)
{
	fetch(function()
	{
		//
	});
};

// Dialog Link
$("#importDb").on("click", function(event)
{
	document.getElementById('data').removeAttribute('style');
	document.getElementById('data').value = '';
	$("#dialog").dialog("open");
	event.preventDefault();
});

$('#dialog').dialog({
	position : {
		my : "center top",
		at : "center top",
		of : "#manage"
	},
	autoOpen : false,
	width : 600,
	buttons : [ {
		text : "Ok",
		click : function()
		{
			if (confirm('This will overwrite all data on this page - continue?'))
			{
				importDb($("#data").val(), function(result)
				{
					$(this).dialog("close");
					if (result)
					{
						window.location.reload();
					}
					else
					{
						alert('The entered data is invalid.');
					}
				});
			}
			else
			{
				$(this).dialog("close");
			}
		}
	}, {
		text : "Cancel",
		click : function()
		{
			$(this).dialog("close");
		}
	} ]
});

document.getElementById('reset').onclick = function(e)
{
	if (confirm('This will discard all changes you ever made on this page - continue?'))
	{
		resetDb();
		window.location.reload();
	}
};

document.getElementById('filterRed').onclick = function(e)
{
	multiSelect(1, 0, true);
};

document.getElementById('filterYellow').onclick = function(e)
{
	multiSelect(2, 0, true);
};

document.getElementById('filterNotRated').onclick = function(e)
{
	multiSelect(3, 0, true);
};

document.getElementById('filterKivaNon').onclick = function(e)
{
	multiSelect(4, 0, true);
};

document.getElementById('filterKivaOne').onclick = function(e)
{
	multiSelect(5, 0, true);
};

document.getElementById('filterKivaTwo').onclick = function(e)
{
	multiSelect(6, 0, true);
};
document.getElementById('filterReligious').onclick = function(e)
{
	multiSelect(7, 0, true);
};

document.getElementById('filterAllOff').onclick = function(e)
{
	multiSelect(20, 0, false);
};

document.getElementById('filterKivaThree').onclick = function(e)
{
	multiSelect(21, 0, false);
};

document.getElementById('filterKivaFour').onclick = function(e)
{
	multiSelect(22, 0, false);
};

document.getElementById('filterKivaFive').onclick = function(e)
{
	multiSelect(23, 0, false);
};

document.getElementById('highlightTop').onclick = function(e)
{
	multiSelect(0, 1, true);
};

document.getElementById('highlightHigh').onclick = function(e)
{
	multiSelect(0, 2, true);
};

document.getElementById('highlightKivaFive').onclick = function(e)
{
	multiSelect(0, 3, true);
};

document.getElementById('highlightNonReligious').onclick = function(e)
{
	multiSelect(0, 4, true);
};

document.getElementById('highlightReligious').onclick = function(e)
{
	multiSelect(0, 5, true);
};
document.getElementById('highlightSocial').onclick = function(e)
{
	multiSelect(0, 6, true);
};

document.getElementById('highlightAllOff').onclick = function(e)
{
	multiSelect(0, 20, false);
};

document.getElementById('highlightNoStarsOff').onclick = function(e)
{
	multiSelect(0, 21, false);
};

document.getElementById('highlightOneStarOff').onclick = function(e)
{
	multiSelect(0, 22, false);
};
document.getElementById('highlightNonReligiousOff').onclick = function(e)
{
	multiSelect(0, 4, false);
};

document.getElementById('highlightReligiousOff').onclick = function(e)
{
	multiSelect(0, 5, false);
};
document.getElementById('highlightSocialOff').onclick = function(e)
{
	multiSelect(0, 6, false);
};
